﻿using Domain.Entitys.DB;
using Domain.IRepositorys.DB;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repositorys.DB
{
    public class Db_AttachmentRepository : IDb_AttachmentRepository
    {
        private readonly MyDbContext myDbContext;

        public Db_AttachmentRepository(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }

        /// <summary>
        /// 添加公共附件信息
        /// </summary>
        /// <param name="db_Attachment"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> AttachmetAddAsync(Db_Attachment db_Attachment)
        {
            await myDbContext.Db_Attachment.AddAsync(db_Attachment);
            return await myDbContext.SaveChangesAsync();
        }

        /// <summary>
        /// 根据编号删除对应公共附件
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public int Attachmetdel(string code)
        {
            int num = 0;
            List<Db_Attachment> db_Attachments = myDbContext.Db_Attachment.Where(a => a.Document_code == code).ToList();
            for (int i = 0; i < db_Attachments.Count; i++)
            {
                myDbContext.Db_Attachment.Remove(db_Attachments[i]);
                num += myDbContext.SaveChanges();

            }
            return num;
        }

        /// <summary>
        /// 根据编号查询对应的附件信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public async Task<List<Db_Attachment>> GetDb_AttachmentsAsync(string code)
        {
            return await myDbContext.Db_Attachment.Where(a => a.Document_code == code).ToListAsync();
        }
    }
}
